package jfinal.demo.common;

import com.jfinal.config.*;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import jfinal.demo.blog.BlogController;
import jfinal.demo.common.model.Company;
import jfinal.demo.common.model._MappingKit;
import jfinal.demo.IndexController;
import jfinal.demo.company.CompanyController;
import jfinal.demo.login.LoginController;
import jfinal.demo.main.MainController;

public class Config extends JFinalConfig {
//
//    public static void main(String[] args){
//        JFinal.start("webapp",8080,"/");
//    }

    public void configConstant(Constants constants) {
        // 加载少量必要配置，随后可用PropKit.get(...)获取值
        PropKit.use("a_little_config.txt");
        constants.setDevMode(PropKit.getBoolean("devMode", false));
    }

    public void configRoute(Routes routes) {
        routes.add("/",IndexController.class);
        routes.add("/blog", BlogController.class);
        routes.add("/login", LoginController.class);
        routes.add("/main", MainController.class);
        routes.add("/company", CompanyController.class);
    }

    public void configEngine(Engine engine) {
        engine.addSharedFunction("/common/_paginate.html");
        engine.addSharedFunction("/common/_lay.html");
//        engine.addSharedFunction("/common/_layout.html");
    }
    public static DruidPlugin createDruidPlugin() {
        return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    }
    public void configPlugin(Plugins plugins) {
        // 配置C3p0数据库连接池插件
        DruidPlugin druidPlugin = createDruidPlugin();
        plugins.add(druidPlugin);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        // 所有映射在 MappingKit 中自动化搞定
        _MappingKit.mapping(arp);
        plugins.add(arp);
    }

    public void configInterceptor(Interceptors interceptors) {

    }

    public void configHandler(Handlers handlers) {
        handlers.add(new ContextPathHandler("cxt"));
    }
}
